/*
 * AppListRender.java
 *
 * Created on 19/Ago/2007, 11:12:27
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mediaLister;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import jmediaplayer.UsefulFunctions;
import jmediaplayer.threads.ThreadLoadMedia;



/**
 *
 * @author Porfirio
 */
public class MediaListerRender extends JComponent implements ListCellRenderer {

	private MediaLister appList;
	private JList list;
	private Object value;
	private int index;
	private boolean isSelected;
	private boolean cellHasFocus;
	public ButtonIcon[] buttonIcons = new ButtonIcon[1];
	public int buttonIconsCount = 0;
	public int selected = 0;
	private Dimension smallD = new Dimension(0, 60);
	private Dimension bigD = new Dimension(0, 95);
	private ImageIcon icNoIcon = new ImageIcon(getClass().getResource("/icons/shot80.png"));
	private ImageIcon icExec = new ImageIcon(getClass().getResource("/icons/exec.png"));
	public String[] data = null;

	public MediaListerRender(MediaLister appList) {
		this.appList = appList;
		final MediaListerRender render=this;
		/*appList.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
			public void mouseMoved(java.awt.event.MouseEvent evt) {
				if (evt.getX()>render.getX() &&
					evt.getY()>render.getY() &&
					evt.getX()<render.getX()+render.getWidth() &&
					evt.getY()<render.getY()+render.getHeight()){
					System.out.println("MediaLister: Item hover.");
				}
				System.out.println(evt.getX() + ">" + render.getX() + " && " +
					evt.getY() + ">" + render.getY() + " && " +
					evt.getX() + "<" + (render.getX()+render.getWidth()) +" && " +
					evt.getY() + "<" + (render.getY()+render.getHeight()));
			}
		});*/

		appList.addMouseListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mouseClicked(java.awt.event.MouseEvent e) {
				//TODO: launch the video
				//if (isSelected) {
					System.out.println("Clicked: " + selected);
					if (e.getClickCount() == 2 && !e.isConsumed()) {
						e.consume();
						//handle double click.
						System.out.println("Double Clicked: " + selected);
					}
				//}
			}
		});


	}

	@Override
	public Component getListCellRendererComponent(JList list, final Object value, int index, boolean isSelected, boolean cellHasFocus) {
		this.list = list;
		this.value = value;
		this.index = index;
		this.isSelected = isSelected;
		this.cellHasFocus = cellHasFocus;
		//if (isSelected) {
			setPreferredSize(bigD);
		//} else {
		//	setPreferredSize(smallD);
		//}

		int id = Integer.parseInt(value.toString(),16);

		//setPreferredSize(new Dimension(0,Math.round(Math.random()*10)));
		ButtonIcon bi =new ButtonIcon(0,id,this,100,10,icExec,0);
		return this;
	}

	@Override
	public void paint(Graphics g) {
		//TODO: recode to expand the info, also need to accompany small/large layout :)
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2d.setPaint(isSelected ? getGradSel() : getGrad());
		g.fillRoundRect(3, 3, getWidth() - 7, getHeight() - 4, 20, 20);
		g.setColor(new Color(Integer.parseInt( "cbcbcb",16)));
		g.drawRoundRect(3, 3, getWidth() - 7, getHeight() - 4, 20, 20);
		g2d.drawImage(icNoIcon.getImage(), 13, 9, this);
		g2d.setColor(Color.BLACK);
		Integer id = Integer.parseInt(value.toString());
		//String[] values = valuest.split("\\|");

		try {
			g2d.drawString(ThreadLoadMedia.mediaData.get(id)[0], 40, 20);
			g2d.drawString(ThreadLoadMedia.mediaData.get(id)[1], 40, 30);
		} catch (NullPointerException ex) {
			//lets ignore it
			//BUG: Why isnt the array being populated as everythings added to the panel?
		}
		ButtonIcon.draw(this,g2d, isSelected);
		if (isSelected) {
			selected = index;
		} else {
		}
	}

	private Paint getGrad() {
		Point2D start = new Point2D.Float(0, 0);
		Point2D end = new Point2D.Float(0, getHeight());
		float[] dist = {0.0f, 1.0f};
		Color[] colors = {new Color(Integer.parseInt( "efefef",16)), new Color(Integer.parseInt( "dddddd",16))};
		LinearGradientPaint p = new LinearGradientPaint(start, end, dist, colors);
		return p;
	}

	private Paint getGradSel() {
		Point2D start = new Point2D.Float(0, 0);
		Point2D end = new Point2D.Float(0, getHeight());
		float[] dist = {0.0f, 1.0f};
		Color[] colors = {new Color(Integer.parseInt( "dddddd",16)), new Color(Integer.parseInt( "c6c6c6",16))};
		LinearGradientPaint p = new LinearGradientPaint(start, end, dist, colors);
		return p;
	}

}

class ButtonIcon {
	private MediaListerRender render;
	private int x;
	private int y;
	private ImageIcon icon;
	private int drawOnSelected; //0 on unselected, 1 on selected, 2 on both

	public ButtonIcon(int index, int id,MediaListerRender render,int x, int y, ImageIcon icon, int drawOnSelected) {
		this.render=render;
		this.x = x;
		this.y = y;
		this.icon = icon;
		this.drawOnSelected = drawOnSelected;
		//render.buttonIcons = (ButtonIcon[])ThreadLoadMedia.resizeArray(render.buttonIcons,ThreadLoadMedia.mediaData.size());

		if (id >= render.buttonIcons.length) {
			render.buttonIcons = (ButtonIcon[])UsefulFunctions.resizeArray(render.buttonIcons,id+1);
		}
		render.buttonIcons[id] = this;
		//System.out.println("MediaLister: Created a icon");
	}

	public ButtonIcon(int index,int id, MediaListerRender render,int x, int y, ImageIcon icon) {
		this(index,id,render,x,y,icon,2);
	}

	public static void draw(MediaListerRender render,Graphics2D g2d, Boolean isSelected) {
		for (int i = 0; i < ThreadLoadMedia.mediaData.size(); i++) {
			ButtonIcon bi=render.buttonIcons[i];
			if (bi!=null && ((isSelected && (bi.drawOnSelected == 1 || bi.drawOnSelected == 2)) || (!isSelected && (bi.drawOnSelected == 0 || bi.drawOnSelected == 2)))) {
				g2d.drawImage(bi.icon.getImage(), bi.x, bi.y, new JButton());
			}
		}
	}

	public static void mouseEv(MediaListerRender render, MouseEvent e){
		for (int i = 0; i < ThreadLoadMedia.mediaData.size(); i++) {
			ButtonIcon bi=render.buttonIcons[i];
			if (bi!=null) {

			}
		}

		//System.out.println(index);
	}

}
